package com.tony.server.controller;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tony.easymvc.mapper.HtmlOperationMapper;
import com.tony.easymvc.mapper.ProjectTablesMapper;
import com.tony.easymvc.po.HtmlOperation;
import com.tony.easymvc.po.ProjectTables;
import com.tony.easymvc.po.ProjectTablesExample;
import com.tony.po.ParaHtmlOperationBean;
import com.tony.server.service.HtmlProjectService;
import com.tony.vo.Pages;
import com.tony.vo.Result;

@Controller
@RequestMapping("stable")
public class SelectTableController {

	@Resource
	ProjectTablesMapper projectTablesMapper;
	@Resource
	HtmlOperationMapper htmlOperationMapper;

	@Resource
	HtmlProjectService htmlProjectService;

	@RequestMapping("list")
	@ResponseBody
	public Result list(Integer id) {
		ProjectTablesExample example = new ProjectTablesExample();
		example.or().andBizIdEqualTo(id);
		List<ProjectTables> item = projectTablesMapper.selectByExample(example);
		ParaHtmlOperationBean paraBeanHtml = new ParaHtmlOperationBean();
		paraBeanHtml.setProjectId(id);
		Set<String> exTables = htmlProjectService.getExistsTables(id);
		if (item == null || item.isEmpty()) {
			return new Result(false, "没有查询到数据", null);
		}
		if (CollectionUtils.isNotEmpty(exTables)) {
			for (ProjectTables projectTables : item) {
				if (exTables.contains(projectTables.getTableName())) {
					projectTables.setIsExists(2);
				}
			}
		}
		return new Result(true, "", item);
	}

	@RequestMapping("delete")
	@ResponseBody
	public String delete(Integer id) {
		// ProjectTables projectTables = projectTablesMapper.selectByPrimaryKey(id);
		// HtmlOperationExample example = new HtmlOperationExample();
		// example.or().andTableNameEqualTo(projectTables.getTableName()).andProjectIdEqualTo(projectTables.getProjectId());
		// try{
		// htmlOperationMapper.deleteByExample(example);
		// projectTablesMapper.deleteByPrimaryKey(id);
		// }catch (Exception e) {
		// e.printStackTrace();
		// }
		return "操作成功";
	}

	@RequestMapping("saveHtmlProjectData")
	@ResponseBody
	public Result batchAddFunction(String tableName, Integer bizId) {
		if (bizId == null || StringUtils.isBlank(tableName)) {
			return new Result(false, "表信息或者项目ID为空", null);
		}
		List<HtmlOperation> item = htmlProjectService.getHtmlOperation(bizId, tableName);
		htmlProjectService.addHtmlOperationBean(item, tableName, bizId);
		return new Result(true, "生成成功", null);
	}
	
	
	@RequestMapping("toOrderDetailsBySn")
	@ResponseBody
	public Result toOrderDetailsBySn(String tableName, Integer bizId) {
		if (bizId == null || StringUtils.isBlank(tableName)) {
			return new Result(false, "表信息或者项目ID为空", null);
		}
		List<HtmlOperation> item = htmlProjectService.getHtmlOperation(bizId, tableName);
		htmlProjectService.addHtmlOperationBean(item, tableName, bizId);
		return new Result(true, "生成成功", null);
	}
	
	

}